|
1
|
|
|
import Memcached from '../extensions/php/memcached' |
|
2
|
|
|
import Memcache from '../services/memcache' |
|
3
|
|
|
import {error, info, success, warning} from '../utils/console' |
|
4
|
|
|
import {client} from '../utils/os' |
|
5
|
|
|
import {getLinkedPhpVersion} from '../utils/phpFpm' |
|
6
|
|
|
|
|
7
|
|
|
class MemcacheController { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Switch the service to the given version. |
|
11
|
|
|
*/ |
|
12
|
|
|
execute = async (status: string): Promise<boolean> => { |
|
13
|
|
|
if (status !== 'on' && status !== 'off') { |
|
14
|
|
|
error('Invalid status. Please provide status \'on\' or \'off\'.') |
|
15
|
|
|
return false |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
const memcache = new Memcache() |
|
19
|
|
|
const phpMemcached = new Memcached() |
|
20
|
|
|
let restart = false |
|
21
|
|
|
|
|
22
|
|
|
if (status === 'on') { |
|
23
|
|
|
restart = await this.enable(memcache, phpMemcached) |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (status === 'off') { |
|
27
|
|
|
restart = await this.disable(memcache, phpMemcached) |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (restart) { |
|
31
|
|
|
const php = await getLinkedPhpVersion() |
|
32
|
|
|
await php.restart() |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return true |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
enable = async (memcache: Memcache, phpMemcached: Memcached): Promise<boolean> => { |
|
39
|
|
|
let restart = false |
|
40
|
|
|
|
|
41
|
|
|
if (await client().packageManager.packageIsInstalled(memcache.service)) { |
|
42
|
|
|
warning(`${memcache.service} is already installed.`) |
|
43
|
|
|
} else { |
|
44
|
|
|
restart = true |
|
45
|
|
|
info(`Installing ${memcache.service}...`) |
|
46
|
|
|
await memcache.install() |
|
47
|
|
|
success(`${memcache.service} has been installed.`) |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
info('Install Memcached PHP extension...') |
|
51
|
|
|
|
|
52
|
|
|
// Memcache is ready, now install the PHP extension. |
|
53
|
|
|
const phpExtensionInstalled = await phpMemcached.install() |
|
54
|
|
|
|
|
55
|
|
|
return restart || phpExtensionInstalled |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
disable = async (memcache: Memcache, phpMemcached: Memcached): Promise<boolean> => { |
|
59
|
|
|
const phpExtensionDisabled = await phpMemcached.disable() |
|
60
|
|
|
|
|
61
|
|
|
if (phpExtensionDisabled) { |
|
62
|
|
|
success('Disabled memcache\'s PHP extension.') |
|
63
|
|
|
} else { |
|
64
|
|
|
warning('Memcache\'s PHP extension was not enabled.') |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!(await client().packageManager.packageIsInstalled(memcache.service))) { |
|
68
|
|
|
warning(`${memcache.service} was not installed.`) |
|
69
|
|
|
return phpExtensionDisabled |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
info(`Uninstalling ${memcache.service}...`) |
|
73
|
|
|
await memcache.uninstall() |
|
74
|
|
|
success(`${memcache.service} has been uninstalled.`) |
|
75
|
|
|
|
|
76
|
|
|
return true |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
export default MemcacheController |